package com.irdstudio.efp.flow.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.efp.flow.common.constance.FlowConstance;
import com.irdstudio.efp.flow.service.dao.InstFlowTaskDao;
import com.irdstudio.efp.flow.service.domain.InstFlowTask;
import com.irdstudio.efp.flow.service.facade.InstFlowTaskService;
import com.irdstudio.efp.flow.service.vo.InstFlowTaskVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;


/**
 * 
 * 
 * 
 * @author Cytus_
 * @since 2018年5月8日 下午7:36:04
 * @version 1.0
 *
 */
@Service("instFlowTaskService")
public class InstFlowTaskServiceImp implements InstFlowTaskService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(InstFlowTaskServiceImp.class);
	
	@Autowired
	private InstFlowTaskDao instFlowTaskDao;
	
	@Override
	public InstFlowTaskVO queryTaskNodeIdAndStatus(InstFlowTaskVO vo) {
		InstFlowTaskVO instFlowTaskVO = null;
		try {
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和状态:"+ vo.getTaskState() +"查询信息开始......");
			//InstFlowTask queryTask = beanCopy(vo, new InstFlowTask());
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("instNodeId", vo.getInstNodeId());
			paramMap.put("taskStatus", Arrays.asList(vo.getTaskState().split(",")));
			InstFlowTask task = instFlowTaskDao.queryTaskNodeIdAndStatus(paramMap);
			instFlowTaskVO = beanCopy(task, new InstFlowTaskVO());
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和状态:"+ vo.getTaskState() +"查询信息结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ vo.getInstNodeId() +"和状态:"+ vo.getTaskState() +"查询信息出现异常!", e);
		}
		return instFlowTaskVO;
	}

	@Override
	public int insertInitTaskByNodeId(InstFlowTaskVO vo) {
		logger.debug("根据节点ID："+ vo.getBizNodeId() +"节点实例化ID："+ vo.getInstNodeId() +"初始化该节点下所有任务开始......");
		int num = 0;
		try {
			InstFlowTask task = beanCopy(vo, new InstFlowTask());
			num = instFlowTaskDao.insertInitTaskByNodeId(task);
			logger.debug("根据节点ID："+ vo.getBizNodeId() +"节点实例化ID："+ vo.getInstNodeId() +"初始化该节点下所有任务结束......");
		} catch (Exception e) {
			logger.debug("根据节点ID："+ vo.getBizNodeId() +"节点实例化ID："+ vo.getInstNodeId() +"初始化该节点下所有任务出现异常!", e);
			num = -1;
		}
		return num;
	}

	@Override
	public InstFlowTaskVO queryFirstTaskByNodeInstId(String nodeInstanceId) {
		InstFlowTaskVO vo = null;
		try {
			logger.debug("根据任务节点编号"+ nodeInstanceId +"查询第一条待执行任务开始......");
			InstFlowTask task = instFlowTaskDao.queryFirstTaskByNodeInstId(nodeInstanceId);
			vo = beanCopy(task, new InstFlowTaskVO());
			logger.debug("根据任务节点编号"+ nodeInstanceId +"查询第一条待执行任务结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ nodeInstanceId +"查询第一条待执行任务出现异常!", e);
		}
		return vo;
	}

	@Override
	public InstFlowTaskVO queryTaskByPreTaskAndInstId(InstFlowTaskVO vo) {
		InstFlowTaskVO instFlowTaskVO = null;
		try {
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和前置任务编号:"+ vo.getPreviousTaskId() +"查询信息开始......");
			InstFlowTask queryTask = beanCopy(vo, new InstFlowTask());
			InstFlowTask task = instFlowTaskDao.queryTaskByPreTaskAndInstId(queryTask);
			instFlowTaskVO = beanCopy(task, new InstFlowTaskVO());
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和前置任务编号:"+ vo.getPreviousTaskId() +"查询信息结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ vo.getInstNodeId() +"和前置任务编号:"+ vo.getPreviousTaskId() +"查询信息出现异常!", e);
		}
		return instFlowTaskVO;
	}

	@Override
	public int updateTaskStateByNodeTaskId(InstFlowTaskVO vo) {
		logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"更新状态信息:"+ vo.getTaskState() +"开始......");
		InstFlowTask task = beanCopy(vo, new InstFlowTask());
		int num = 0;
		try {
			//处理错误信息 最大长度位200
			String errorMsg = task.getErrorMsg();
			if (StringUtil.isNotEmpty(errorMsg) && task.getErrorMsg().length() > 200) {
				task.setErrorMsg(task.getErrorMsg().substring(0, 200));
			}
			num = instFlowTaskDao.updateTaskStateByNodeTaskId(task);
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"更新状态信息:"+ vo.getTaskState() +"结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"更新状态信息:"+ vo.getTaskState() +"出现异常!", e);
			num = -1;
		}
		return num;
	}

	@Override
	public InstFlowTaskVO queryTaskByNodeTaskId(InstFlowTaskVO vo) {
		logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"查询任务信息开始......");
		InstFlowTaskVO instFlowTaskVO = null;
		try {
			InstFlowTask queryTask = beanCopy(vo, new InstFlowTask());
			InstFlowTask task = instFlowTaskDao.queryTaskByNodeTaskId(queryTask);
			instFlowTaskVO = beanCopy(task, new InstFlowTaskVO());
			logger.debug("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"查询任务信息结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ vo.getInstNodeId() +"和任务编号:"+ vo.getTaskId() +"查询任务信息出现异常!", e);
		}
		return instFlowTaskVO;
	}

	@Override
	public int countTaskByNodeInstId(String instNodeId) {
		logger.debug("根据任务节点编号"+ instNodeId +"统计已初始化的任务信息开始......");
		int num = 0;
		try {
			num = instFlowTaskDao.countTaskByNodeInstId(instNodeId);
			logger.debug("根据任务节点编号"+ instNodeId +"统计已初始化的任务信息结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ instNodeId +"统计已初始化的任务信息出现异常!", e);
			num = -1;
		}
		return num;
	}

	@SuppressWarnings("unchecked")
	public List<InstFlowTaskVO> queryPageShowPluginTaskInfoByInstNodeId(String instNodeId) {
		
		logger.debug("节点实例编号:"+ instNodeId +"页面插件查询开始......");
		List<InstFlowTaskVO> list = null;
		
		Map<String, Object> queryFinishTask = new HashMap<String, Object>();
		queryFinishTask.put("instNodeId", instNodeId);
		queryFinishTask.put("bizPluginType", Arrays.asList(FlowConstance.PLUGIN_TYPE_PAGE_ADD, FlowConstance.PLUGIN_TYPE_PAGE_UPDATE, FlowConstance.PLUGIN_TYPE_PAGE_VIEW));
		queryFinishTask.put("taskState", FlowConstance.TASK_STATUS_END);
		
		List<InstFlowTask> tasks = new ArrayList<InstFlowTask>();
		logger.debug("节点实例编号:"+ instNodeId +", 已完成页面插件任务查询开始......");
		List<InstFlowTask> finishTasks = instFlowTaskDao.queryPageShowTaskInfo(queryFinishTask);
		if (Objects.nonNull(finishTasks)) {
			tasks.addAll(finishTasks);
			logger.debug("节点实例编号:"+ instNodeId +", 已完成页面插件任务查询结束, 记录条数:"+ finishTasks.size());
		}
		
		Map<String, Object> queryWaitTask = new HashMap<String, Object>();
		queryWaitTask.put("instNodeId", instNodeId);
		queryWaitTask.put("bizPluginType", Arrays.asList(FlowConstance.PLUGIN_TYPE_PAGE_ADD, FlowConstance.PLUGIN_TYPE_PAGE_UPDATE, FlowConstance.PLUGIN_TYPE_PAGE_VIEW));
		queryWaitTask.put("taskState", FlowConstance.TASK_STATUS_PAUSE);
		
		String taskId = null;
		logger.debug("节点实例编号:"+ instNodeId +", 等待处理页面插件任务查询开始......");
		List<InstFlowTask> waitTasks = instFlowTaskDao.queryPageShowTaskInfo(queryWaitTask);
		if (Objects.nonNull(waitTasks) && !waitTasks.isEmpty()) {
			tasks.addAll(waitTasks);
			taskId = waitTasks.get(0).getTaskId();
			logger.debug("节点实例编号:"+ instNodeId +", 等待处理页面插件任务查询结束, 记录条数:"+ finishTasks.size());
		}
		
		logger.debug("节点实例编号:"+ instNodeId +", 联系页面插件处理任务查询开始......");
		while (StringUtil.isNotEmpty(taskId)) {
			
			Map<String, Object> queryInitTask = new HashMap<String, Object>();
			queryInitTask.put("instNodeId", instNodeId);
			queryInitTask.put("bizPluginType", Arrays.asList(FlowConstance.PLUGIN_TYPE_PAGE_ADD, FlowConstance.PLUGIN_TYPE_PAGE_UPDATE, FlowConstance.PLUGIN_TYPE_PAGE_VIEW));
			queryInitTask.put("taskState", FlowConstance.TASK_STATUS_READY);
			queryInitTask.put("previousTaskId", taskId);
			
			List<InstFlowTask> initTask = instFlowTaskDao.queryPageShowTaskInfo(queryInitTask);
			if (Objects.nonNull(initTask) && !initTask.isEmpty()) {
				tasks.addAll(initTask);
				taskId = initTask.get(0).getTaskId();
			} else {
				taskId = null;
			}
		}
		logger.debug("节点实例编号:"+ instNodeId +", 联系页面插件处理任务查询结束......");
		try {
			list = (List<InstFlowTaskVO>) beansCopy(tasks, InstFlowTaskVO.class);
			logger.debug("节点实例编号:"+ instNodeId +"页面插件查询结束......");
		} catch (Exception e) {
			logger.debug("节点实例编号:"+ instNodeId +"页面插件查询出现异常!", e);
		}
		return list;
	}

	public List<InstFlowTaskVO> queryByInstNodeId(String instNodeId) {
		List<InstFlowTaskVO> vos = null;
		logger.debug("根据任务节点编号"+ instNodeId +"查询已初始化的任务信息开始......");
		try {
			List<InstFlowTask> tasks = instFlowTaskDao.queryByInstNodeId(instNodeId);
			vos = (List<InstFlowTaskVO>) beansCopy(tasks, InstFlowTaskVO.class);
			logger.debug("根据任务节点编号"+ instNodeId +"查询已初始化的任务信息结束......");
		} catch (Exception e) {
			logger.error("根据任务节点编号"+ instNodeId +"查询已初始化的任务信息出现异常!", e);
		}
		return vos;
	}

	public int deleteByPk(String instNodeId){
		logger.debug("删除数据开始......");
		int num = 0;
		try {
			num = this.instFlowTaskDao.deleteByPk(instNodeId);
			logger.debug("删除数据结束......");
		} catch (Exception e) {
			logger.debug("删除数据失败......");
			num = -1;
		}
		return num;
	}

}
